package com.example.tyz1201.haha.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import com.example.tyz1201.haha.Adapter.JokesAdapter;
import com.example.tyz1201.haha.Model.Content;
import com.example.tyz1201.haha.Model.Data;
import com.example.tyz1201.haha.Model.JokesContent;
import com.example.tyz1201.haha.Model.JokesData;
import com.example.tyz1201.haha.NetworkRequests;
import com.example.tyz1201.haha.R;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by tyz1201 on 2016/4/14.
 */
public class InflateActivity extends AppCompatActivity {

    private String line = "", data = "";
    private int position;
    private List<JokesData> datas;
    private JokesContent jokesContent;
    private ListView lv_jokes;
    private JokesAdapter jokesAdapter;
    private NetworkRequests networkRequests;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inflate);

        Intent intent = getIntent();
        position = intent.getIntExtra("position", -1);
        Log.d("position", position + "");

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("position", position + "/");
//                getDataFromSomewhere();

                try {
                    networkRequests = new NetworkRequests(new URL("http://japi.juhe.cn/funny/list.from?cat=" + position + "&st=0&count=20&key=9137b9d52f086cbe85101d66fdd9fdb9"), data, line);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                data = networkRequests.getDataFromSomewhere();
                Gson gson = new Gson();
                jokesContent = gson.fromJson(data, JokesContent.class);

                datas = jokesContent.getResult().getData();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        lv_jokes = (ListView) findViewById(R.id.lv_jokes);
                        jokesAdapter = new JokesAdapter(datas, InflateActivity.this);
                        lv_jokes.setAdapter(jokesAdapter);
                    }
                });

            }
        }).start();
    }


//    private void getDataFromSomewhere() {
//        try {
//            String urlStr = "http://japi.juhe.cn/funny/list.from?cat=" + position + "&st=0&count=20&key=9137b9d52f086cbe85101d66fdd9fdb9";
//            Log.d("url",urlStr);
//            URL url = new URL(urlStr);
//            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//            conn.setRequestMethod("GET");
//
//            InputStream is = conn.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is);
//            BufferedReader br = new BufferedReader(isr);
//
//
//            while ((line = br.readLine()) != null) {
//                data = line + "\n";
//            }
//            Log.d("data", data);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
